package com.orangelife.mobile.point.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.point.adapter.MyPointDetailAdapter;
import com.orangelife.mobile.point.biz.MyPointBiz;
import com.orangelife.mobile.surrounding.biz.SurroundHttpBiz;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyPointDetailActivity extends PullRefreshBaseActivity {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_title_right)
    TextView tvTitleRight;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    @Bind(R.id.lvVoucher)
    PullToRefreshListView pullListView;
    private MyPointDetailAdapter<Map<String, Object>> adapter;
    private List<Map<String, Object>> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_score_detail);
        ButterKnife.bind(this);
        initView();
        setListener();
        setAdapter();
        setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, adapter);
    }

    private void setAdapter() {
        data = new ArrayList<>();
        adapter = new MyPointDetailAdapter<>(this, data);
    }

    private void setListener() {
    }

    private void initView() {
        tvTitle.setText(R.string.my_score_detail);
        tvTitleRight.setVisibility(View.INVISIBLE);
    }

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MyPointBiz.POINTS_DETAIL:
                    ArrayList list = (ArrayList) JsonReponseHandler.getListFromJson(
                            String.valueOf(msg.obj), adapter, null);
                    int total = SurroundHttpBiz.getGoodListTotal(msg.obj);
                    handleAfterRequest(isConnected(), total, list, pullListView, adapter);
                    break;
                case 500:
                    dismissProgress();
                    ToastHelper.getInstance()._toast(
                            msg.obj.toString());
                    break;
                case 1011:
                    dismissProgress();
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return data;
    }

    @Override
    protected int getDataCount() {
        return data.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        HashMap<String, String> map = new HashMap<>();
        map.put("pageIndex", pageNo + "");
        map.put("pageSize", pageSize + "");
        MyPointBiz.getMyPointsDetail(map,handler);
    }
}
